# -*- coding:utf-8 -*-
"""
复合策略
"""
from function.interface import *
import numpy as np
import talib

class Strategy8(JyInterface):
    symbol = 'btcusdt'
    period = '1min'    
    size = 100
    cash = 1000
    periodShort = 10
    periodLong  = 20
    periodRSI = 12
    donchianNum = 20
    oversold = 30  # 超卖
    overbought = 70  # 超买
    flag = 0
    def __init__(self, **wargs):
        super(Strategy8, self).__init__(**wargs)
    
    def loop(self):
        """策略部分"""
        kline = self.getKline(self.symbol, self.period, self.size)
        maShort = talib.MA(kline['closes'], timeperiod=self.periodShort)
        maLong  = talib.MA(kline['closes'], timeperiod=self.periodLong)
        rsi = talib.RSI(kline['closes'], timeperiod=self.periodRSI)
        donchianHigh = max(kline['highs'][self.donchianNum-1:-1])
        donchianLow = min(kline['lows'][self.donchianNum-1:-1]) 
        lastprice = self.getLastPrice(self.symbol)
        highprice = kline['highs']
        lowprice = kline['lows']
        position = self.getPositionSt(self.symbol)
        if self.futureOrSpot == 0:
            if position == 0 and highprice[-2]< highprice[-1] and maShort[-1] > maLong[-1] and rsi[-1] < self.overbought and self.flag == 0:
                self.buy(self.symbol, lastprice, self.cash/lastprice)
                self.flag = 2
            elif position > 0 and lowprice[-1] < lowprice[-2] and maShort[-1] < maLong[-1] and rsi[-1] > self.oversold and self.flag == 2:
                self.sell(self.symbol, lastprice, position)
                self.flag = 0
            if position == 0 and lastprice > donchianHigh and self.flag == 0:
                self.buy(self.symbol, lastprice, self.cash/lastprice)
                self.flag = 1
            elif position > 0 and lastprice < donchianLow and self.flag == 1:
                self.sell(self.symbol, lastprice, position)
                self.flag = 0
        if self.futureOrSpot == 1:
            if highprice[-2] < highprice[-1] and maShort[-1] > maLong[-1] and rsi[-1] < self.overbought:
                if position == 0 and self.flag == 0:
                    self.buy(self.symbol, lastprice, self.cash/lastprice)
                    self.flag = 2
                elif position < 0 and self.flag == 2:
                    self.cover(self.symbol, lastprice, -position)
                    self.flag = 0
            elif lowprice[-1] < lowprice[-2] and maShort[-1] < maLong[-1] and rsi[-1] > self.oversold:
                if position > 0 and self.flag == 2:
                    self.sell(self.symbol, lastprice, position)
                    self.flag = 0
                elif position == 0 and self.flag == 0:
                    self.short(self.symbol, lastprice, self.cash/lastprice)
                    self.flag = 2
            if lastprice > donchianHigh:
                if position == 0 and self.flag == 0:
                    self.buy(self.symbol, lastprice, self.cash/lastprice)
                    self.flag = 1
                elif position < 0 and self.flag == 1:
                    self.cover(self.symbol, lastprice, -position)
                    self.flag = 0                    
            elif lastprice < donchianLow:
                if position > 0 and self.flag == 1:
                    self.sell(self.symbol, position, lastprice)
                    self.flag = 0
                elif position == 0 and self.flag == 0:
                    self.short(self.symbol, lastprice, self.cash/lastprice)
                    self.flag = 1
            
        

if __name__ == "__main__":
    st = Strategy8(type_=1,futureOrSpot=0,appKey='e1937',secret='bf36bc08f5',passphrase='',transferId=101)
    st.run(st.loop)